//
//  GHAboutUsViewController.m
//  GodHorses
//
//  Created by Mac on 2017/11/16.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "TMGOrderDetailViewController.h"
#import "LBPassWordPresentViewController.h"
@interface TMGOrderDetailViewController ()
@property(nonatomic, strong) NSArray<STTableEasyModel*>                     *datasouce;
@end

@implementation TMGOrderDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"订单详情 ";
    self.tableView.st_noreslutView = [[STNoresultView alloc] initWithFrame:CGRectMake(0, 100, UIScreenWidth, 400) title:@"暂无详情" buttonTitle:@"" buttonHandle:^(NSString *titleString) {
        
    }];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self sendOrderDetailRequest];
    }];
    [self.tableView.mj_header beginRefreshing];
    
    // Do any additional setup after loading the view.
}
- (void)configDataSouce{
    self.datasouce = @[
                       [[STTableEasyModel alloc] initWithTextString:@"交易状态"
                                                       detailString:self.model.status
                                                      accessoryType:UITableViewCellAccessoryNone],
                       [[STTableEasyModel alloc] initWithTextString:@"交易类型"
                                                       detailString:self.model.trade_type_name accessoryType:UITableViewCellAccessoryNone],
                       [[STTableEasyModel alloc] initWithTextString:@"订单时间"
                                                       detailString:self.model.create_at
                                                      accessoryType:UITableViewCellAccessoryNone],
                       [[STTableEasyModel alloc] initWithTextString:@"订单说明"
                                                       detailString:self.model.tmg_description
                                                      accessoryType:UITableViewCellAccessoryNone],
                       [[STTableEasyModel alloc] initWithTextString:@"订单号"
                                                       detailString:self.model.order_no
                                                      accessoryType:UITableViewCellAccessoryNone],
                       [[STTableEasyModel alloc] initWithTextString:@"订单金额"
                                                       detailString:self.model.price
                                                      accessoryType:UITableViewCellAccessoryNone],
                       [[STTableEasyModel alloc] initWithTextString:@"到期时间"
                                                       detailString:self.model.create_at
                                                      accessoryType:UITableViewCellAccessoryNone],
                       ];
    
    if ([self.model.status isEqualToString:@"待支付"]) {
        __weak typeof(self) weakSelf =  self;
        UIView * footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 100)];
        footer.backgroundColor = self.tableView.backgroundColor;
        STButton * confimButton = [[STButton alloc] initWithFrame:CGRectMake(30, 0, UIScreenWidth - 60 , 44)
                                                            title:@"立即支付"
                                                       titleColor:[UIColor whiteColor]
                                                        titleFont:18
                                                     cornerRadius:10
                                                  backgroundColor:TM_orangeColor
                                                  backgroundImage:nil
                                                            image:nil];
        [confimButton setClicAction:^(UIButton *sender) {
            [weakSelf onSelectedPayButton];
        }];
        confimButton.st_centerY = 50;
        confimButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        [footer addSubview:confimButton];
        self.tableView.tableFooterView = footer;
    }else{
        self.tableView.tableFooterView = nil;
    }
}
#pragma mark --subView
- (void)configSubView{
    UIView * header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 190)];
    header.backgroundColor = self.tableView.backgroundColor;
    
    UIImageView * logImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 35, 63, 66)];
    logImageView.clipsToBounds = YES;
    logImageView.layer.cornerRadius = 5;
    logImageView.st_centerX = UIScreenWidth / 2;
    logImageView.image = [UIImage imageNamed:@"订单支付成功"];
    //    logImageView.backgroundColor = [UIColor colorWithRed:55/255.0 green:60/255.0 blue:156/255.0 alpha:1];
    [header addSubview:logImageView];
    
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(0, logImageView.st_bottom + 15, 120, 18)
                                                     text:@"交易成功"
                                                textColor:TM_secendTextColor
                                                     font:15
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentCenter];
    titleLable.st_centerX = UIScreenWidth / 2;
    titleLable.numberOfLines = 0;
    [NSString lableAutoAdjustheightWithLabel:titleLable];
    [header addSubview:titleLable];
    self.tableView.tableHeaderView = header;
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.datasouce.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 44;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.01;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseIdentifier];
        
    }
    cell.textLabel.textColor = UIColorFromRGBA(0x333333);
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    cell.detailTextLabel.font = [UIFont systemFontOfSize:13];
    cell.detailTextLabel.textColor = UIColorFromRGBA(0x999999);
    STTableEasyModel * model = self.datasouce[indexPath.row];
    cell.textLabel.text = model.textString;
    cell.detailTextLabel.text = model.detailString;
    if (model.accessoryView) {
        cell.accessoryView = model.accessoryView;
    }else{
        cell.accessoryType = model.accessoryType;
    }
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}
#pragma mark --Action Method
- (void)onSelectedPayButton{
    [self presentInputPwdViewControllerWithOrder:self.model.oid.description];
}
- (void)presentInputPwdViewControllerWithOrder:(NSString*)order{
    LBPassWordPresentViewController * vc = [LBPassWordPresentViewController new];
    [vc setPwdDidEdited:^(LBPassWordPresentViewController *passVC ,NSString *inputPwd) {
        [self sendBuyRequestWithOid:order pwd:inputPwd handle:^(NSString *errorDesc) {
            if (!errorDesc.length) {
                [SVProgressHUD showSuccessWithStatus:@"支付成功"];
                [passVC dismissWithHandle:^{
                    [self.navigationController popViewControllerAnimated:YES];
                }];
            }else{
                if ([errorDesc isEqualToString:@"交易密码不正确!"]) {
                    passVC.alertLable.text = @"密码错误";
                    passVC.alertLable.hidden = NO;
                    [passVC rightLablebackAnimation];
                }else{
                    [SVProgressHUD showErrorWithStatus:errorDesc];
                }
                
            }
        }];
    }];
    [self presentViewController:vc animated:NO completion:nil];
}
#pragma mark --NetWork Method
- (void)sendOrderDetailRequest{
    //1买金2卖金3投资4赎回6充值7提现
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    if ([STUserManger defult].userID.length) {
        [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }
    if ([STUserManger defult].token.length) {
        [dic setObject:[STUserManger defult].token forKey:@"token"];
    }
    [dic setObject:self.model.trade_type forKey:@"trade_type"];
    [dic setObject:self.model.orderID.description forKey:@"oid"];
    NSString * url = [NSString stringWithFormat:@"%@%@",service_header,@"/gold/public/api/order/getOrderDetail"];
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [SVProgressHUD dismiss];
                                                 NSDictionary * modelDic = responseObject[@"data"];
                                                 TMGOrderModel * model = [TMGOrderModel mj_objectWithKeyValues:modelDic];
                                                 model.oid = self.model.oid;
                                                 self.model = model;
                                                 [self configDataSouce];
                                                 [self.tableView reloadData];
                                                 [self.tableView.mj_header endRefreshing];
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [SVProgressHUD dismiss];
                                                 
                                                 [self.tableView reloadData];
                                                 //[SVProgressHUD showErrorWithStatus:error.desc];
                                                 [self.tableView.mj_header endRefreshing];
                                             }];
}
- (void)sendBuyRequestWithOid:(NSString*)oid pwd:(NSString*)pwd handle:(void(^)(NSString * errorDesc))handle{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    NSString * url = [NSString stringWithFormat:@"%@%@",service_header,@"/gold/public/api/money/balancePay"];
    if ([STUserManger defult].userID.length) {
        [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }
    if ([STUserManger defult].token.length) {
        [dic setObject:[STUserManger defult].token forKey:@"token"];
    }
    if (oid.length) {
        [dic setObject:oid forKey:@"oid"];
    }
    if (pwd.length) {
        [dic setObject:pwd forKey:@"pay_password"];
    }
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 if (handle) {
                                                     handle(@"");
                                                 }
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [SVProgressHUD dismiss];
                                                 if (handle) {
                                                     handle(error.desc);
                                                 }
                                             }];
}
@end



